package com.example.city_defense.sprites;

import com.badlogic.androidgames.framework.Pixmap;
import com.example.city_defense.Asset;
import com.example.city_defense.sprites.Vehicle.VehicleState;

public class Bullet {

	public int x, y;
	public Pixmap image;
	public int TYPE;
	Vehicle vehicle;
	private int v;
	private float deltaX;
	private float deltaY;
	private float delta;
	public State state;
	public int attack;
	private int desX;
	private int desY;

	public enum State {
		Attack, Explode, OutScreen, Finish
	};

	public Bullet(int x, int y, int TYPE, Vehicle vehicle) {
		// TODO Auto-generated constructor stub
		this.x = x;
		this.y = y;
		this.setTarget(vehicle);
		this.TYPE = TYPE;
		this.load();
		this.state = State.Attack;
		this.update(0.01f);
		this.attack = 10;
	}

	public void setTarget(Vehicle vehicle) {
		this.vehicle = vehicle;
	}

	public Vehicle getTarget() {
		return this.vehicle;
	}

	public Vehicle getVehicle() {
		return this.vehicle;
	}

	public void update(float deltaTime) {
		if (this.TYPE != 2 && this.TYPE != 3) {
			// State of Bullet is ATACK ==================================
			if (this.state == State.Attack) {
				if (this.vehicle != null) {
					this.deltaX = vehicle.x - this.x;
					this.deltaY = vehicle.y - this.y;
					this.delta = (float) Math.sqrt(Math.pow(deltaX, 2)
							+ Math.pow(deltaY, 2));
					if (vehicle.state == VehicleState.Running) {
						if ((delta - vehicle.image.getHeight() / 2) < v
								* deltaTime) {
							this.state = State.Explode;
						} else {
							x += v * (deltaX / delta) * deltaTime;
							y += v * (deltaY / delta) * deltaTime;
						}
					} else {
						this.vehicle = null;
					}
				} else {
					x += v * (deltaX / delta) * deltaTime;
					y += v * (deltaY / delta) * deltaTime;
				}
			}
			// State of Bullet is EXPLODE ===============================
			else if (this.state == State.Explode) {
				if (this.vehicle != null) {
					this.vehicle.updateBlood(this.attack);
				}

				this.state = State.Finish;
			}
			if (this.x < (0 - this.image.getWidth()) || this.x > 800
					|| this.y > 875 || this.y < (0 - this.image.getHeight())) {
				this.state = State.OutScreen;
			}
		}else if(this.TYPE == 2){
			this.deltaX = vehicle.x - this.x;
			this.deltaY = vehicle.y - this.y;
			this.delta = (float) Math.sqrt(Math.pow(deltaX, 2)
					+ Math.pow(deltaY, 2));
		}else if (this.TYPE == 3){
			
		}

	}

	public void load() {
		switch (TYPE) {
		case 1:
			this.image = Asset.bullet1;
			this.v = 50;
			break;
		case 2:
			this.image = Asset.bullet2;
			this.v = 50;
			this.desX = (int) this.vehicle.x;
			this.desY = (int) this.vehicle.y;
			break;
		case 3:
			this.image = Asset.bullet3;
			this.v = 50;
			break;
		case 4:
			this.image = Asset.bullet4;
			this.v = 50;
			break;
		case 5:
			this.image = Asset.bullet5;
			this.v = 50;
			break;
		case 6:
			this.image = Asset.bullet6;
			this.v = 50;
			break;
		case 7:
			this.image = Asset.bullet7;
			this.v = 50;
			break;
		case 8:
			this.image = Asset.bullet8;
			this.v = 50;
			break;
		case 9:
			this.image = Asset.bullet9;
			this.v = 50;
			break;
		case 10:
			this.image = Asset.bullet10;
			this.v = 50;
			break;
		default:
			break;
		}
	}
	// public boolean checkCollision(){
	// if((this.x >= (vehicle.x - vehicle.image.getWidth()/2))
	// &&(this.x <= (vehicle.x + vehicle.image.getWidth()/2))
	// &&(this.y >=( vehicle.y - vehicle.image.getHeight()/2))
	// &&(this.y <= vehicle.y - vehicle.image.getHeight()/2)){
	// return true;
	// }
	// return false;
	// }

}
